#include <iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<string.h>
#include<limits>
//不建议服务器绑定特定的ip地址
int main(int argc,char *argv[])
{
    if(argc!=3)
    {
        std::cerr<<"usage fail"<<std::endl;
    }
     std::string serverip = argv[1];
    uint16_t serverport = std::stoi(argv[2]);
    //创建套接字
     int socketid = socket(AF_INET,SOCK_DGRAM,0);
     if(socketid<0)
     {
        //此处应该打印错误信息
     }
     //客户端是否要显示绑定的问题：客户端不用显示的绑定，客户端随即绑定，
     //防止客户端端口冲突，但是服务器必须显式的绑定，才能正常运行
     struct sockaddr_in server;
     memset(&server,0,sizeof(server));
     server.sin_family = AF_INET;
     server.sin_port = htons(serverport);
     server.sin_addr.s_addr = inet_addr(serverip.c_str());

     while(true)
     {
        std:: string input;
        std:: cout<<"please input message"<<std::endl;
        fflush(stdin);
        std::getline(std::cin,input);
        int n = sendto(socketid,input.c_str(),input.size(),0,(struct sockaddr*)&server,sizeof(server));
        (void)n;
        char buffer[1024];
        struct sockaddr_in server2;
        socklen_t len =sizeof(server2);
        int m = recvfrom(socketid,buffer,sizeof(buffer)-1,0,(struct sockaddr*)&server2,&len);
        if(m>0)
        {
         std::cout<<buffer<<std::endl;
        }    
      }

}